/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpr.tads.Rent.view;

import br.ufpr.tads.Rent.controller.Automovel;
import br.ufpr.tads.Rent.controller.EnumClass.*;
import br.ufpr.tads.Rent.model.VeiculoDAO;
import br.ufpr.tads.Rent.model.VeiculoDTO;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author G0006185
 */
public final class VeiculoView extends javax.swing.JFrame {
    private categoria categoria;
    private  ModeloTabelaVeiculo tabelaveiculo;
    private final VeiculoDAO auto;

    /**
     * Creates new form VeiculoView
     */
    public VeiculoView() throws SQLException, IOException, FileNotFoundException, ClassNotFoundException  {
        
        auto = new VeiculoDAO();
        
        tabelaveiculo = new ModeloTabelaVeiculo();
        populaTabela();
        initComponents();
        
    }
    public void populaTabela() throws SQLException{
        
        tabelaveiculo.setListaVeiculos(auto.findVeiculo());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        Tipo = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        buttonGroup5 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableV = new javax.swing.JTable();
        cboMarca = new javax.swing.JComboBox();
        cboEstado = new javax.swing.JComboBox();
        cboModelo = new javax.swing.JComboBox();
        cobCategoria = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtAno = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        bntNew = new javax.swing.JButton();
        cboTipo = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtPlaca = new javax.swing.JTextField();
        txtValor = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        bntUpdate = new javax.swing.JButton();
        bntDel = new javax.swing.JButton();

        setDefaultCloseOperation(VeiculoView.DISPOSE_ON_CLOSE);

        tableV.setModel(tabelaveiculo);
        tableV.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableVMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableV);

        cboMarca.setModel(new DefaultComboBoxModel<>(marca.values()));

        cboEstado.setModel(new DefaultComboBoxModel<>(estado.values()));

        cboModelo.setModel(new DefaultComboBoxModel<>(ModeloAutomovel.values()));

        cobCategoria.setModel(new DefaultComboBoxModel<>(categoria.values()));

        jLabel1.setText("Marca");

        jLabel2.setText("Estado");

        jLabel3.setText("Modelo");

        jLabel4.setText("Categoria");

        txtAno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAnoActionPerformed(evt);
            }
        });

        jLabel5.setText("Ano");

        bntNew.setText("Salvar");
        bntNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntNewActionPerformed(evt);
            }
        });

        cboTipo.setModel(new DefaultComboBoxModel<>(tipo.values()));
        cboTipo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboTipoItemStateChanged(evt);
            }
        });
        cboTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboTipoActionPerformed(evt);
            }
        });

        jLabel6.setText("Tipo");

        jLabel7.setText("Placa");

        jLabel8.setText("Valor");

        bntUpdate.setText("Atualizar");
        bntUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntUpdateActionPerformed(evt);
            }
        });

        bntDel.setText("Deletar");
        bntDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntDelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addGap(20, 20, 20))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cobCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboMarca, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboEstado, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(cboModelo, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtPlaca)
                            .addComponent(cboTipo, 0, 89, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel8)
                                .addComponent(jLabel5)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtValor)
                            .addComponent(txtAno))))
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bntUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bntNew, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bntDel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(94, 94, 94))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cobCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(cboTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel7)
                            .addComponent(txtPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(cboEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(cboModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5)
                                .addComponent(txtAno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(bntNew)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bntUpdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bntDel)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtAnoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAnoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtAnoActionPerformed

    private void bntNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntNewActionPerformed
        // TODO add your handling code here:
         
       VeiculoDTO veiculo = new VeiculoDTO();
       veiculo.setAno(Integer.parseInt(txtAno.getText()));
       veiculo.setCategoria(categoria.values()[cobCategoria.getSelectedIndex()]);
       veiculo.setEstado(cboEstado.getSelectedIndex());
       veiculo.setMarca(marca.values()[cboMarca.getSelectedIndex()]);
       veiculo.setModelo(cboModelo.getSelectedIndex());
       veiculo.setPlaca(txtPlaca.getText());
       veiculo.setValorCompra(Double.parseDouble(txtValor.getText()));
       veiculo.setTipo(tipo.values()[cboTipo.getSelectedIndex()]);
        try {
            auto.addVeiculo(veiculo);
        } catch (SQLException ex) {
            Logger.getLogger(VeiculoView.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            populaTabela();
        } catch (SQLException ex) {
            Logger.getLogger(VeiculoView.class.getName()).log(Level.SEVERE, null, ex);
        }
     
    }//GEN-LAST:event_bntNewActionPerformed

    private void cboTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboTipoActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_cboTipoActionPerformed

    private void cboTipoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboTipoItemStateChanged
        // TODO add your handling code here:
        if(cboTipo.getSelectedIndex()==0){
        cboModelo.setModel(new DefaultComboBoxModel<>(ModeloAutomovel.values()));
        }else if(cboTipo.getSelectedIndex()==1){
            cboModelo.setModel(new DefaultComboBoxModel<>(modelomotocicleta.values()));
        }else if(cboTipo.getSelectedIndex()==2){
            cboModelo.setModel(new DefaultComboBoxModel<>(modelovan.values()));
        }
    }//GEN-LAST:event_cboTipoItemStateChanged

    private void tableVMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableVMouseClicked
     
        cobCategoria.setSelectedItem(tableV.getValueAt(tableV.getSelectedRow(), 2));
        cboMarca.setSelectedItem(tableV.getValueAt(tableV.getSelectedRow(), 1));
        cboEstado.setSelectedItem(tableV.getValueAt(tableV.getSelectedRow(), 7));
        cboModelo.setSelectedItem(tableV.getValueAt(tableV.getSelectedRow(), 4));
        cboTipo.setSelectedItem(tableV.getValueAt(tableV.getSelectedRow(), 6));
        txtPlaca.setText(tableV.getValueAt(tableV.getSelectedRow(), 5).toString());
        txtValor.setText(tableV.getValueAt(tableV.getSelectedRow(), 8).toString());
        txtAno.setText(tableV.getValueAt(tableV.getSelectedRow(), 3).toString());
        
    }//GEN-LAST:event_tableVMouseClicked

    private void bntUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntUpdateActionPerformed
     if(tableV.getSelectedRow()!=-1){  
        VeiculoDTO veiculo = new VeiculoDTO();
       veiculo.setAno(Integer.parseInt(txtAno.getText()));
       veiculo.setCategoria(categoria.values()[cobCategoria.getSelectedIndex()]);
       veiculo.setEstado(cboEstado.getSelectedIndex());
       veiculo.setMarca(marca.values()[cboMarca.getSelectedIndex()]);
       veiculo.setModelo(cboModelo.getSelectedIndex());
        System.out.println(cboModelo.getSelectedIndex());
       veiculo.setPlaca(txtPlaca.getText());
       veiculo.setValorCompra(Double.parseDouble(txtValor.getText()));
       veiculo.setTipo(tipo.values()[cboTipo.getSelectedIndex()]);
       veiculo.setId(Integer.parseInt(tableV.getValueAt(tableV.getSelectedRow(), 0).toString()));
        try {
            auto.updateVeiculo(veiculo);
        } catch (SQLException ex) {
            Logger.getLogger(VeiculoView.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            populaTabela();
        } catch (SQLException ex) {
            Logger.getLogger(VeiculoView.class.getName()).log(Level.SEVERE, null, ex);
        }
     }else{
          JOptionPane.showMessageDialog(null,"Selecione um Linha!", "Erro", JOptionPane.ERROR_MESSAGE);
     }
     
    }//GEN-LAST:event_bntUpdateActionPerformed

    private void bntDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntDelActionPerformed
        
   if(tableV.getSelectedRow()!=-1){    
        try {
            auto.delVeiculo(Integer.parseInt(tableV.getValueAt(tableV.getSelectedRow(), 0).toString()));
            populaTabela();
        } catch (SQLException ex) {
                      JOptionPane.showMessageDialog(null,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);

        }
      }else{
          JOptionPane.showMessageDialog(null,"Selecione um Linha!", "Erro", JOptionPane.ERROR_MESSAGE);
     }   
        
        
    }//GEN-LAST:event_bntDelActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.ButtonGroup Tipo;
    private javax.swing.JButton bntDel;
    private javax.swing.JButton bntNew;
    private javax.swing.JButton bntUpdate;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.ButtonGroup buttonGroup5;
    private javax.swing.JComboBox cboEstado;
    private javax.swing.JComboBox cboMarca;
    private javax.swing.JComboBox cboModelo;
    private javax.swing.JComboBox cboTipo;
    private javax.swing.JComboBox cobCategoria;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableV;
    private javax.swing.JTextField txtAno;
    private javax.swing.JTextField txtPlaca;
    private javax.swing.JTextField txtValor;
    // End of variables declaration//GEN-END:variables
}
